<template>
  <div>
    <div class="i-layout-page-header">
      <div class="i-layout-page-header">
        <span class="ivu-page-header-title">{{ $route.meta.title }}</span>
      </div>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Form
		  ref="roleData"
		  :model="roleData"
		  :label-width="labelWidth"
		  :label-position="labelPosition"
		  @submit.native.prevent
		>
		  <Row type="flex" :gutter="24">
			<Col v-bind="grid">
			  <FormItem label="分类名称：" label-for="keywords">
				<Input
				  search
				  enter-button
				  placeholder="请输入分类名称关键字"
				  v-model="formValidate.keywords"
				  @on-search="userSearchs"
				/>
			  </FormItem>
			</Col>
		  </Row>
		  <Row type="flex">
			<Col v-bind="grid">
			  <Button type="primary" icon="md-add" @click="add">新增</Button>
			</Col>
		  </Row>
      </Form>
      <Table
        :columns="columns1"
        :data="categoryList"
        ref="table"
        class="mt25"
        :loading="loading"
        highlight-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
	    <template slot-scope="{ row, index }" slot="id">
	      {{index+1}}
	    </template>
	    <template slot-scope="{ row, index }" slot="status">
	      <span v-if="row.status == 1">启动</span>
	      <span v-else>停用</span>
	    </template>
	    <template slot-scope="{ row, index }" slot="action">
	      <a @click="edit(row)">修改</a>
	      <Divider type="vertical" />
	      <a  @click="del(row)">删除</a>
	    </template>
      </Table>
      <div class="acea-row row-right page">
        <Page :total="total" show-elevator show-total @on-change="pageChange" :page-size="groupFrom.limit" />
      </div>
    </Card>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import { categoryAddApi, categoryEditApi, categoryListApi, statusApi } from '@/api/cms';
export default {
  name: 'user_group',
  data() {
    return {
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
	  formValidate: {
		  status: '',
		  page: 1,
		  limit: 20,
		  type: 0,
		  keywords:''
		},
      columns1: [
		{
		  title: '序号',
		  slot: 'id',
		  align:'center',
		  width: 80,
		},
		{
		  title: '分类名称',
		  key: 'title',
		  align:'center',
		  minWidth: 130,
		},
		{
		  title: '分类描述',
		  key: 'intr',
		  align:'center',
		  minWidth: 130,
		},
		{
		  title: '状态',
		  slot: 'status',
		  align:'center',
		  minWidth: 130,
		},
		{
		  title: '操作',
		  slot: 'action',
		  fixed: 'right',
		  minWidth: 120,
		},
      ],
      roleData: {
        is_show: '',
        keyword: '',
      },
      groupFrom: {
        page: 1,
        limit: 15,
      		keywords:''
      },
      categoryList: [],
      total: 0,
    };
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : 75;
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'left';
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加
	add() {
	  this.$modalForm(categoryAddApi()).then(() => this.getList());
	  // categoryAddApi().then(async res => {
	  //     this.$refs.edits.modals = true;
	  //     this.FromData = res.data;
	  // }).catch(res => {
	  //     this.$Message.error(res.msg);
	  // })
	},
	// 编辑
	edit(row) {
	  this.$modalForm(categoryEditApi(row.id)).then(() => this.getList());
	  // categoryEditApi(row.id).then(async res => {
	  //     this.FromData = res.data;
	  //     this.$refs.edits.modals = true;
	  // }).catch(res => {
	  //     this.$Message.error(res.msg);
	  // })
	},
	// 删除
	del(row) {
	  let delfromData = {
		title: '删除分类',
		num: 0,
		url: `cms/category/${row.id}`,
		method: 'DELETE',
		ids: '',
	  };
	  this.$modalSure(delfromData)
		.then((res) => {
		  this.$Message.success(res.msg);
		  this.getList();
		})
		.catch((res) => {
		  this.$Message.error(res.msg);
		});
	},
    // 列表
    getList() {
      this.loading = true;
		this.formValidate.status = this.status === 'all' ? '' : this.status;
		categoryListApi(this.formValidate)
		  .then(async (res) => {
			let data = res.data;
			this.categoryList = data.list;
			this.total = data.count;
			this.loading = false;
		  })
		  .catch((res) => {
			this.loading = false;
			this.$Message.error(res.msg);
		  });
    },
    pageChange(index) {
      this.groupFrom.page = index;
      this.getList();
    },
	userSearchs(){
		this.formValidate.page = 1;
		this.getList();
		console.info(this.formValidate)
	}
  },
};
</script>

<style scoped lang="stylus"></style>
